package com.gel.shortLink.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gel.shortLink.dao.entity.LinkDO;
import com.gel.shortLink.dao.mapper.LinkDOMapper;
import com.gel.shortLink.dto.req.RecycleBinRecoverReqDTO;
import com.gel.shortLink.dto.req.RecycleBinRemoveReqDTO;
import com.gel.shortLink.dto.req.RecycleBinSaveReqDTO;
import com.gel.shortLink.dto.req.RecyclePageReqDTO;
import com.gel.shortLink.dto.resp.ShortLinkPageRespDTO;
import com.gel.shortLink.service.RecycleService;
import com.gel.shortLink.util.LinkUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static com.gel.shortLink.common.constant.RedisCacheConstant.GOTO_IS_NULL_SHORT_LINK_KEY;
import static com.gel.shortLink.common.constant.RedisCacheConstant.GOTO_SHORT_LINK_KEY;

@Service
@RequiredArgsConstructor
public class RecycleServiceImpl extends ServiceImpl<LinkDOMapper, LinkDO> implements RecycleService {
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public void save(RecycleBinSaveReqDTO requestParam) {
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, requestParam.getGid())
                .eq(LinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(LinkDO::getDelFlag, 0)
                .eq(LinkDO::getEnableStatus, 0);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(linkDO, updateWrapper);
        //删除缓存
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()) );

    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(RecyclePageReqDTO requestParam) {
        LambdaQueryWrapper<LinkDO> queryWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .in(LinkDO::getGid, requestParam.getGidList())
                .eq(LinkDO::getDelFlag, 0)
                .eq(LinkDO::getEnableStatus, 1);
        IPage<LinkDO> pageLinks = baseMapper.selectPage(requestParam, queryWrapper);
       return pageLinks.convert(each -> {
            ShortLinkPageRespDTO bean = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            return bean;
        });
    }

    @Override
    public void recover(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, requestParam.getGid())
                .eq(LinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(LinkDO::getDelFlag, 0)
                .eq(LinkDO::getEnableStatus, 1);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(linkDO, updateWrapper);
        stringRedisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public void remove(RecycleBinRemoveReqDTO requestParam) {
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, requestParam.getGid())
                .eq(LinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(LinkDO::getDelFlag, 0)
                .eq(LinkDO::getEnableStatus, 1);
        baseMapper.delete(updateWrapper);
    }

}
